package com.stock.demo.function.selfselect.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.stock.demo.R;
import com.stock.demo.adapter.AnimatorAdapter;
import com.stock.demo.adapter.SelfSelectMenuAdapter;
import com.stock.demo.framework.ui.fragment.BaseNetFragment;
import com.stock.demo.function.selfselect.presenter.SelfSelectPresenter;
import com.stock.demo.function.selfselect.presenter.SelfSelectPresenterImpl;
import com.stock.demo.model.SelfSelectMenuBean;
import com.stock.demo.model.StockBean;
import com.stock.demo.util.SettingUtils;

import java.util.ArrayList;

/**
 * Created by XuZhaoyu on 2017/6/1.
 */

public class SelfSelectFragment extends BaseNetFragment implements SelfSelectView {

    private View mView;
    private LinearLayout mLlMenu;
    private TextView mTvMenuBtn;
    private DoubleRecyclerView mRvSelfSelectList;

    private SelfSelectPresenter mPresenter;
    private RecyclerView mRvMenu;
    private ArrayList<SelfSelectMenuBean> mMenuBeen = new ArrayList<>();
    private SelfSelectMenuAdapter mMenuAdapter;
    private Runnable mRunRefresh;
    private boolean mShouldRefresh;//判断是否需要刷新数据（是否停留在当前页面，不在当前页面就终止刷新）
    private boolean mShouldWait;//判断是否在时间间隔内（是否在刷新数据的间隔时间内，需要等待，如每3000MS发起一次网络请求数据）
    private boolean mIsViewCreated;//判断该页面是否已经绘制完毕

    @Override
    public View onCreateSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_self_select, container, false);
        return mView;
    }

    @Override
    public void onReload(View v) {

    }

    @Override
    public void initView() {
        mLlMenu = (LinearLayout) mView.findViewById(R.id.ll_self_select_menu);
        mTvMenuBtn = (TextView) mView.findViewById(R.id.tv_self_select_menu_btn);
        mRvMenu = (RecyclerView) mView.findViewById(R.id.rv_self_select_menu);
        mRvSelfSelectList = (DoubleRecyclerView) mView.findViewById(R.id.rv_self_select_list);
        mMenuBeen.add(new SelfSelectMenuBean("我的自选", true));
        mMenuBeen.add(new SelfSelectMenuBean("我的持仓", false));
        mMenuBeen.add(new SelfSelectMenuBean("每日一看", false));
        mRvMenu.setLayoutManager(new LinearLayoutManager(getContext()));
        mMenuAdapter = new SelfSelectMenuAdapter(getContext(), mMenuBeen);
        mRvMenu.setAdapter(mMenuAdapter);
        mTvMenuBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOrHideMenu();
            }
        });
        mIsViewCreated = true;
    }

    @Override
    public void initData() {
        onNetNext(new Object()).setNetComplete();
        initPresent();
        mRunRefresh = new Runnable() {
            @Override
            public void run() {
                mShouldWait = false;
                mPresenter.getStockInfo();
                System.out.println("selfSelect refresh");
            }
        };
        shouldRefresh(true);//第一次进入页面，让页面开始加载数据
    }

    private void showOrHideMenu() {
        if (mLlMenu.getVisibility() == View.VISIBLE) {
            hideMenu();
        } else {
            showMenu();
        }
    }

    private void hideMenu() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mLlMenu, "Alpha", 1.0f, 0f);
        animator.setDuration(300);
        animator.addListener(new AnimatorAdapter() {

            @Override
            public void onAnimationStart(Animator animation) {
                mTvMenuBtn.setClickable(false);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mLlMenu.setVisibility(View.GONE);
                mTvMenuBtn.setClickable(true);
            }
        });
        animator.start();
    }

    private void showMenu() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mLlMenu, "Alpha", 0f, 1.0f);
        animator.setDuration(300);
        animator.addListener(new AnimatorAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mTvMenuBtn.setClickable(false);
                mLlMenu.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mTvMenuBtn.setClickable(true);
            }
        });
        animator.start();
    }

    @Override
    public void onGetStockInfo(ArrayList<StockBean> selectBeen) {
        mRvSelfSelectList.setData(selectBeen);
        if ((!mShouldWait) && mShouldRefresh) {
            mShouldWait = true;
            mRvSelfSelectList.postDelayed(mRunRefresh, SettingUtils.getRefreshSpeed());
        }
    }

    public void shouldRefresh(Boolean shouldRefresh) {
        if (mIsViewCreated) {
            mShouldRefresh = shouldRefresh;
            if (mShouldRefresh) {
                mPresenter.getStockInfo();
                System.out.println("selfSelect refresh");
            }
        }
    }

    private void initPresent() {
        if (mPresenter == null) {
            mPresenter = new SelfSelectPresenterImpl(this);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRvSelfSelectList.removeCallbacks(mRunRefresh);
    }
}
